package com.seg.system.service.impl;

import com.seg.common.exception.ServiceException;
import com.seg.common.utils.DateUtils;
import com.seg.common.utils.bean.BeanValidators;
import com.seg.system.SelectDataI;
import com.seg.system.domain.TOutWarehouse;
import com.seg.system.domain.TSparePart;
import com.seg.system.mapper.TOutWarehouseMapper;
import com.seg.system.mapper.TSparePartMapper;
import com.seg.system.service.ITOutWarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Validator;
import java.util.ArrayList;
import java.util.List;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2023-06-29
 */
@Service
public class TOutWarehouseServiceImpl implements ITOutWarehouseService {
    @Autowired
    private TOutWarehouseMapper tOutWarehouseMapper;
    @Autowired
    private TSparePartMapper tSparePartMapper;
    @Autowired
    protected Validator validator;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public TOutWarehouse selectTOutWarehouseById(Long id) {
        return tOutWarehouseMapper.selectTOutWarehouseById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param tOutWarehouse 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<TOutWarehouse> selectTOutWarehouseList(TOutWarehouse tOutWarehouse) {
        return tOutWarehouseMapper.selectTOutWarehouseList(tOutWarehouse);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param tOutWarehouse 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertTOutWarehouse(TOutWarehouse tOutWarehouse, String operName) {
        BeanValidators.validateWithException(validator, tOutWarehouse);
        tOutWarehouse.setCreateTime(DateUtils.getNowDate());
        tOutWarehouse.setCreateBy(operName);
        tOutWarehouse.setDelFlag("0");
        tOutWarehouse.setStatus(0);
        //判断物料状态1，如果当前库存低于安全库存时 ；物料状态2，如果当前库存低于SAP库存时
        //根据物料号查询数据
        TSparePart t = tSparePartMapper.getPartByMaterialNo(tOutWarehouse.getMaterialNo());
        if (t == null) {
            return -1;
        }
        if (t.getStock() == 0L) {
            //库存为0 返回数据提示库存不足
            return -2;
        }
        if (t.getStock() < tOutWarehouse.getOutNum()) {
            //库存量小于出库数量 返回数据提示库存不足
            return -2;
        }
        Long curStock = t.getStock() - tOutWarehouse.getOutNum();
        if (curStock < t.getSapStock()) {
            t.setStatus2("CLSAP");
        }
        if (curStock < t.getSafetyStock()) {
            t.setStatus1("CLS");
        }
        t.setStock(curStock);
        int i = tSparePartMapper.updateTSparePart(t);
        int j = tOutWarehouseMapper.insertTOutWarehouse(tOutWarehouse);
        if (i == 0 || j == 0) {
            throw new ServiceException("出库失败");
        }
        return 1;
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param tOutWarehouse 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateTOutWarehouse(TOutWarehouse tOutWarehouse) {
        tOutWarehouse.setUpdateTime(DateUtils.getNowDate());
        return tOutWarehouseMapper.updateTOutWarehouse(tOutWarehouse);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteTOutWarehouseByIds(Long[] ids) {
        return tOutWarehouseMapper.deleteTOutWarehouseByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteTOutWarehouseById(Long id) {
        return tOutWarehouseMapper.deleteTOutWarehouseById(id);
    }

    public List<SelectDataI> getStatus() {
        List<SelectDataI> list = new ArrayList<>();
        List<Long> station = new ArrayList<>();
        station.add(0L);
        station.add(1L);
        station.add(2L);
        station.add(3L);
        for (Long s : station) {
            SelectDataI selectData = new SelectDataI();
            selectData.setName(formatStatus(s));
            selectData.setValue(s);
            list.add(selectData);
        }
        return list;
    }

    private String formatStatus(Long s) {
        String str = "";
        if (s == 0L) {
            str = "出库";
        } else if (s == 1L) {
            str = "待一级审批";
        } else if (s == 2L){
            str = "待二级审批";
        }else if(s== 3L){
            str= "退库";
        }
        return str;
    }
}
